package cn.com.dhcc.turbo.service.registry;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.turbo.dao.registry.DataCategoryDao;
import cn.com.dhcc.turbo.dao.registry.DomainInfoDao;
import cn.com.dhcc.turbo.dao.registry.NodeInfoDao;
import cn.com.dhcc.turbo.dao.registry.PriorityConfDao;
import cn.com.dhcc.turbo.entity.registry.DataCategory;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.registry.PriorityConf;
/**
 * 优先级配置
 * @日期：2014-9-12上午11:45:01
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public class PriorityConfService extends BaseService {
	
	@Autowired
	private NodeInfoDao nodeInfoDao;
	
	@Autowired
	private DomainInfoDao domainInfoDao;
	@Autowired
	private PriorityConfDao confDao;
	@Autowired
	private DataCategoryDao dcDao;
	/**
	 * 初始化数据树
	 * @param type 1：按发送数据分类　2：按接收系统　3：按接收节点　4：按发送系统　5：按业务域
	 * @return
	 * @throws ServiceException
	 */
	public String initDataTree(String type) throws ServiceException {
		String dataTree ="";
		try {
			if("1".equals(type)){//1：按发送数据分类
				dataTree = this.getSendDataCategoryTree();
			}else if("2".equals(type)){//2：按接收系统
				dataTree =  this.getRecvSysRootTree();
			}else if("3".equals(type)){//3：按接收节点
				dataTree =  this.getRecvNodeTree();
			}else if("4".equals(type)){//4：按发送系统
				dataTree =  this.getSendSysTree();
			}else if("5".equals(type)){//5：按业务域
				dataTree =  this.getDomainTree();
			}
		} catch (ServiceException e) {
			throw new ServiceException("获取数据树失败", e);
		}
		return dataTree;
	}
	/**
	 * 1.发送数据分类
	 * @return
	 * @throws ServiceException
	 */
	public String getSendDataCategoryTree() throws ServiceException {
		StringBuffer nodeTreeData = new StringBuffer();
		try {
//			NodeInfo localNode = nodeInfoDao.getLocalNode();
			nodeTreeData.append("[");
			nodeTreeData.append("{");
			nodeTreeData.append("id:'").append("").append("',");
			nodeTreeData.append("pId:'").append("").append("',");
			nodeTreeData.append("name:'").append("发送数据分类").append("',");
			nodeTreeData.append("nocheck:").append(true).append(",");
			nodeTreeData.append("checked:").append(false).append(",");
			nodeTreeData.append("icon:'\'+_rs_image_path+\'/rsroot.png',");
			nodeTreeData.append("open:").append(true);
			nodeTreeData.append("}");
			List<DomainInfo> list = domainInfoDao.getAllForDataCategory();
			for (Iterator<DomainInfo> iterator = list.iterator(); iterator.hasNext();) {
				DomainInfo vo = iterator.next();
				boolean checked = false;
				boolean isOpen = false;
//				boolean noCheck = true;
				boolean isParent = false;
				nodeTreeData.append(",");
				nodeTreeData.append("{");
				nodeTreeData.append("id:'").append(vo.getId()).append("',");
				nodeTreeData.append("pId:'").append("").append("',");
				nodeTreeData.append("name:'").append(vo.getName()).append("',");
				nodeTreeData.append("type:'").append(1).append("',");
				nodeTreeData.append("select:'").append(false).append("',");
				nodeTreeData.append("nocheck:").append(true).append(",");
				nodeTreeData.append("checked:").append(checked).append(",");
				nodeTreeData.append("open:").append(isOpen).append(",");
				if (!"".equals(vo.getCont()) && Integer.parseInt(vo.getCont()) > 0 ) {
					isParent = true;
				}
				nodeTreeData.append("icon:'\'+_rs_image_path+\'/domain.png',");
				nodeTreeData.append("count:'").append(vo.getCont()).append("',");
				nodeTreeData.append("isParent:").append(isParent);
				nodeTreeData.append("}");
				/*List <SysInfo> syslist = sysInfoDao.getAllForPriorityConfDataCategory(vo.getId(),localNode.getId());
				for (Iterator<SysInfo> sysIterator = syslist.iterator(); sysIterator
						.hasNext();) {
					SysInfo sysInfo =  sysIterator.next();
					boolean sysChecked = false;
					boolean sysIsOpen = false;
					boolean sysIsParent = false;
					nodeTreeData.append(",");
					nodeTreeData.append("{");
					nodeTreeData.append("id:'").append(sysInfo.getId()).append("',");
					nodeTreeData.append("pId:'").append(vo.getId()).append("',");
					nodeTreeData.append("name:'").append(sysInfo.getSysName()).append("',");
					nodeTreeData.append("select:'").append("0").append("',");
					nodeTreeData.append("type:'").append(1).append("',");
					nodeTreeData.append("select:'").append(false).append("',");
					nodeTreeData.append("nocheck:").append(true).append(",");
					nodeTreeData.append("checked:").append(sysChecked).append(",");
					nodeTreeData.append("open:").append(sysIsOpen).append(",");
					if (sysInfo.getCont() != null  && Integer.parseInt(sysInfo.getCont()) > 0 ) {
						sysIsParent = true;
					}
					nodeTreeData.append("icon:'\'+_rs_image_path+\'/sys.png',");
					nodeTreeData.append("count:'").append(sysInfo.getCont()).append("',");
					nodeTreeData.append("isParent:").append(sysIsParent);
					nodeTreeData.append("}");
				}*/
			}
		} catch (DaoException e) {
			throw new ServiceException("获取发送数据分类树失败", e);
		}
		nodeTreeData.append("]");
		return nodeTreeData.toString();
	} 
	/**
	 * 1.2获取子数据分类树
	 * @param pid
	 * @return
	 * @throws ServiceException
	 */
	public String sendDataCategoryTree(String id) throws ServiceException {
		StringBuilder nodeTreeData = new StringBuilder();
		
		try {
			List<DataCategory> list = dcDao.getListByPid(id);
			nodeTreeData.append("[");
			for (Iterator<DataCategory> iterator = list.iterator(); iterator.hasNext();) {
				DataCategory vo = iterator.next();
				boolean isParent = false;
				PriorityConf conf = confDao.getByOwnId(vo.getId());
				nodeTreeData.append("{");
				nodeTreeData.append("id:'").append(vo.getId()).append("',");
				nodeTreeData.append("pId:'").append(vo.getPid()).append("',");
				nodeTreeData.append("name:'").append(vo.getDataName()).append("',");
				nodeTreeData.append("type:'").append(1).append("',");
				nodeTreeData.append("select:'").append(true).append("',");
				nodeTreeData.append("nocheck:").append(true).append(",");
				nodeTreeData.append("checked:").append(false).append(",");
				
				nodeTreeData.append("open:").append(false).append(",");
				if (!"".equals(vo.getCount()) && Integer.parseInt(vo.getCount()) > 0 ) {
					isParent = true;
				}
				nodeTreeData.append("icon:'\'+_rs_image_path+\'/dc.png',");
				if(conf != null && "1".equals(conf.getStatus())){
					nodeTreeData.append("icon:'\'+_rs_image_path+\'/task.png',");
				}
				nodeTreeData.append("count:'").append(vo.getCount()).append("',");
				nodeTreeData.append("isParent:").append(isParent);
				nodeTreeData.append("}");
				if (iterator.hasNext())
					nodeTreeData.append(",");
			}
			nodeTreeData.append("]");
		} catch (DaoException e) {
			throw new ServiceException("获取子数据分类树失败", e);
		}
		return nodeTreeData.toString();
	}
	/**
	 * 1.3获取子数据分类树--所选节点为业务域
	 * @param domainId
	 * @return
	 * @throws ServiceException
	 */
	public String dataDomainCategoryTree(String domainId) throws ServiceException {
		StringBuilder nodeTreeData = new StringBuilder();
		try {
			List<DataCategory> list = dcDao.getListByDomainId(domainId);
			nodeTreeData.append("[");
			for (Iterator<DataCategory> iterator = list.iterator(); iterator.hasNext();) {
				DataCategory vo = iterator.next();
				PriorityConf conf = confDao.getByOwnId(vo.getId());
				boolean isParent = false;
				nodeTreeData.append("{");
				nodeTreeData.append("id:'").append(vo.getId()).append("',");
				nodeTreeData.append("pId:'").append(vo.getPid()).append("',");
				nodeTreeData.append("name:'").append(vo.getDataName()).append("',");
				nodeTreeData.append("type:'").append(1).append("',");
				nodeTreeData.append("select:'").append(true).append("',");
				nodeTreeData.append("nocheck:").append(true).append(",");
				nodeTreeData.append("icon:'\'+_rs_image_path+\'/dc.png',");
				if(conf != null && "1".equals(conf.getStatus())){
					nodeTreeData.append("icon:'\'+_rs_image_path+\'/task.png',");
				}
				nodeTreeData.append("checked:").append(false).append(",");
				nodeTreeData.append("open:").append(false).append(",");
				if (!"".equals(vo.getCount()) && Integer.parseInt(vo.getCount()) > 0 ) {
					isParent = true;
				}
				nodeTreeData.append("count:'").append(vo.getCount()).append("',");
				nodeTreeData.append("isParent:").append(isParent);
				nodeTreeData.append("}");
				if (iterator.hasNext())
					nodeTreeData.append(",");
			}
			nodeTreeData.append("]");
		} catch (DaoException e) {
			throw new ServiceException("获取子数据分类树失败", e);
		}
		return nodeTreeData.toString();
	}
	/**
	 * 2.接收系统树(根：节点)
	 * @return
	 * @throws ServiceException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getRecvSysRootTree() throws ServiceException {
		StringBuffer nodeTreeData = new StringBuffer();
		try {
			NodeInfo localNode = nodeInfoDao.getLocalNode();
			nodeTreeData.append("[");
			nodeTreeData.append("{");
			nodeTreeData.append("id:'").append("").append("',");
			nodeTreeData.append("pId:'").append("").append("',");
			nodeTreeData.append("name:'").append("接收系统").append("',");
			nodeTreeData.append("nocheck:").append(true).append(",");
			nodeTreeData.append("checked:").append(false).append(",");
			nodeTreeData.append("icon:'\'+_rs_image_path+\'/rsroot.png',");
			nodeTreeData.append("open:").append(true);
			nodeTreeData.append("}");
			List list = nodeInfoDao.getAllLocalExchNodeList(localNode.getId());
			for (Iterator<NodeInfo> iterator = list.iterator(); iterator.hasNext();) {
				NodeInfo vo = iterator.next();
				boolean checked = false;
				boolean isOpen = false;
				boolean noCheck = true;
				boolean isParent = false;
				if (Integer.parseInt(vo.getCont()) > 0 ) {
					isParent = true;
				}
				nodeTreeData.append(",");
				nodeTreeData.append("{");
				nodeTreeData.append("id:'").append(vo.getId()).append("',");
				nodeTreeData.append("pId:'").append("").append("',");
				nodeTreeData.append("name:'").append(vo.getNodeName()).append("',");
				nodeTreeData.append("type:'").append(2).append("',");
				nodeTreeData.append("select:").append(false).append(",");
				nodeTreeData.append("nocheck:").append(noCheck).append(",");
				nodeTreeData.append("checked:").append(checked).append(",");
				nodeTreeData.append("icon:'\'+_rs_image_path+\'/node02.png',");
				nodeTreeData.append("open:").append(isOpen).append(",");
				nodeTreeData.append("isParent:").append(isParent);
				nodeTreeData.append("}");
			}
		} catch (DaoException e) {
			throw new ServiceException("获取接收系统树失败", e);
		}
		nodeTreeData.append("]");
		return nodeTreeData.toString();
	} 
	
	/**
	 * 2.1接收系统树(子：系统)
	 * @return
	 * @throws ServiceException
	 */
	public String getRecvSysTree(String id) throws ServiceException {
		StringBuilder nodeTreeData = new StringBuilder();
		//try {
			/*List<SysInfo> list = sysInfoDao.getSysInfoByNodeId(id);
			nodeTreeData.append("[");
			for (Iterator<SysInfo> iterator = list.iterator(); iterator.hasNext();) {
				SysInfo vo = iterator.next();
				PriorityConf conf = confDao.getByOwnId(vo.getId());
				nodeTreeData.append("{");
				nodeTreeData.append("id:'").append(vo.getId()).append("',");
				nodeTreeData.append("pId:'").append("").append("',");
				nodeTreeData.append("name:'").append(vo.getSysName()).append("',");
				nodeTreeData.append("type:'").append(2).append("',");
				nodeTreeData.append("select:").append(true).append(",");
				nodeTreeData.append("nocheck:").append(true).append(",");
				nodeTreeData.append("checked:").append(false).append(",");
				if(conf != null && "1".equals(conf.getStatus())){
					nodeTreeData.append("icon:'\'+_rs_image_path+\'/task.png',");
				}
				nodeTreeData.append("open:").append(false).append(",");
				nodeTreeData.append("}");
				if (iterator.hasNext())
					nodeTreeData.append(",");
			}
			nodeTreeData.append("]");*/
		/*} catch (DaoException e) {
			throw new ServiceException("获取接收系统树失败", e);
		}*/
		return nodeTreeData.toString();
	}
	
	/**
	 * 3.按接收节点树
	 * @return
	 * @throws ServiceException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getRecvNodeTree() throws ServiceException {
		StringBuffer nodeTreeData = new StringBuffer();
		try {
			NodeInfo localNode = nodeInfoDao.getLocalNode();
			nodeTreeData.append("[");
			nodeTreeData.append("{");
			nodeTreeData.append("id:'").append("").append("',");
			nodeTreeData.append("pId:'").append("").append("',");
			nodeTreeData.append("name:'").append("接收节点").append("',");
			nodeTreeData.append("nocheck:").append(true).append(",");
			nodeTreeData.append("checked:").append(false).append(",");
			nodeTreeData.append("icon:'\'+_rs_image_path+\'/rsroot.png',");
			nodeTreeData.append("open:").append(true);
			nodeTreeData.append("}");
			List list = nodeInfoDao.getAllLocalExchNodeList(localNode.getId());
			for (Iterator<NodeInfo> iterator = list.iterator(); iterator.hasNext();) {
				NodeInfo vo = iterator.next();
				PriorityConf conf = confDao.getByOwnId(vo.getId());
				boolean checked = false;
				boolean isOpen = false;
//				boolean noCheck = true;
				boolean isParent = false;
				nodeTreeData.append(",");
				nodeTreeData.append("{");
				nodeTreeData.append("id:'").append(vo.getId()).append("',");
				nodeTreeData.append("pId:'").append("").append("',");
				nodeTreeData.append("name:'").append(vo.getNodeName()).append("',");
				nodeTreeData.append("type:'").append(3).append("',");
				nodeTreeData.append("select:").append(true).append(",");
				nodeTreeData.append("nocheck:").append(true).append(",");
				nodeTreeData.append("checked:").append(checked).append(",");
				if(conf != null && "1".equals(conf.getStatus())){
					nodeTreeData.append("icon:'\'+_rs_image_path+\'/task.png',");
				}
				nodeTreeData.append("open:").append(isOpen).append(",");
				nodeTreeData.append("isParent:").append(isParent);
				nodeTreeData.append("}");
			}
		} catch (DaoException e) {
			throw new ServiceException("获取接收节点树失败", e);
		}
		nodeTreeData.append("]");
		return nodeTreeData.toString();
	} 
	/**
	 * 4.发送系统树
	 * @return
	 * @throws ServiceException
	 */
	public String getSendSysTree() throws ServiceException {
		StringBuffer nodeTreeData = new StringBuffer();
		try {
//			NodeInfo localNode = nodeInfoDao.getLocalNode();
			nodeTreeData.append("[");
			nodeTreeData.append("{");
			nodeTreeData.append("id:'").append("").append("',");
			nodeTreeData.append("pId:'").append("").append("',");
			nodeTreeData.append("name:'").append("发送系统").append("',");
			nodeTreeData.append("nocheck:").append(true).append(",");
			nodeTreeData.append("checked:").append(false).append(",");
			nodeTreeData.append("icon:'\'+_rs_image_path+\'/rsroot.png',");
			nodeTreeData.append("open:").append(true);
			nodeTreeData.append("}");
			/*List<SysInfo> list = sysInfoDao.getLocalSysInfoList();
			for (Iterator<SysInfo> iterator = list.iterator(); iterator.hasNext();) {
				SysInfo vo = iterator.next();
				PriorityConf conf = confDao.getByOwnId(vo.getId());
				boolean checked = false;
				boolean isOpen = false;
				boolean isParent = false;
				nodeTreeData.append(",");
				nodeTreeData.append("{");
				nodeTreeData.append("id:'").append(vo.getId()).append("',");
				nodeTreeData.append("pId:'").append("").append("',");
				nodeTreeData.append("name:'").append(vo.getSysName()).append("',");
				nodeTreeData.append("type:'").append(4).append("',");
				nodeTreeData.append("select:").append(true).append(",");
				nodeTreeData.append("nocheck:").append(true).append(",");
				nodeTreeData.append("checked:").append(checked).append(",");
				if(conf != null && "1".equals(conf.getStatus())){
					nodeTreeData.append("icon:'\'+_rs_image_path+\'/task.png',");
				}
				nodeTreeData.append("open:").append(isOpen).append(",");
				nodeTreeData.append("isParent:").append(isParent);
				nodeTreeData.append("}");
			}*/
		} catch (Exception e) {
			throw new ServiceException("获取发送系统树失败", e);
		}
		nodeTreeData.append("]");
		return nodeTreeData.toString();
	} 
	/**
	 * 5.业务域树
	 * @return
	 * @throws ServiceException
	 */
	public String getDomainTree() throws ServiceException {
		StringBuffer nodeTreeData = new StringBuffer();
		try {
			nodeTreeData.append("[");
			nodeTreeData.append("{");
			nodeTreeData.append("id:'").append("").append("',");
			nodeTreeData.append("pId:'").append("").append("',");
			nodeTreeData.append("name:'").append("业务域").append("',");
			nodeTreeData.append("nocheck:").append(true).append(",");
			nodeTreeData.append("checked:").append(false).append(",");
			nodeTreeData.append("icon:'\'+_rs_image_path+\'/rsroot.png',");
			nodeTreeData.append("open:").append(true);
			nodeTreeData.append("}");
			List<DomainInfo> list = domainInfoDao.getAll();
			for (Iterator<DomainInfo> iterator = list.iterator(); iterator.hasNext();) {
				DomainInfo vo = iterator.next();
				PriorityConf conf = confDao.getByOwnId(vo.getId());
				boolean checked = false;
				boolean isOpen = false;
				boolean isParent = false;
				nodeTreeData.append(",");
				nodeTreeData.append("{");
				nodeTreeData.append("id:'").append(vo.getId()).append("',");
				nodeTreeData.append("pId:'").append("").append("',");
				nodeTreeData.append("name:'").append(vo.getName()).append("',");
				nodeTreeData.append("type:'").append(5).append("',");
				nodeTreeData.append("select:").append(true).append(",");
				nodeTreeData.append("nocheck:").append(true).append(",");
				nodeTreeData.append("checked:").append(checked).append(",");
				if(conf != null && "1".equals(conf.getStatus())){
					nodeTreeData.append("icon:'\'+_rs_image_path+\'/task.png',");
				}
				nodeTreeData.append("open:").append(isOpen).append(",");
				nodeTreeData.append("isParent:").append(isParent);
				nodeTreeData.append("}");
			}
		} catch (DaoException e) {
			throw new ServiceException("获取业务域树失败", e);
		}
		nodeTreeData.append("]");
		return nodeTreeData.toString();
	}

	public String dataTree(String type, String id,boolean select) throws ServiceException {
		String dataTree ="";
		try {
			if("1".equals(type)){//1：按发送数据分类
				dataTree=this.sendDataCategoryTree(id);
			}else if("2".equals(type)){//2：按接收系统
				dataTree =  this.getRecvSysTree(id);
			}else if("3".equals(type)){//3：按接收节点
				
			}else if("4".equals(type)){//4：按发送系统
				
			}else if("5".equals(type)){//5：按业务域
				
			}
		} catch (ServiceException e) {
			throw new ServiceException("获取数据树失败", e);
		}
		return dataTree;
	}
	/**
	 * 获取优先级配置
	 * @param ownId 对象id
	 * @return
	 * @throws ServiceException
	 */
	public PriorityConf getByOwnId(String ownId) throws ServiceException {
		try {
			return confDao.getByOwnId(ownId);
		} catch (DaoException e) {
			throw new ServiceException("获取优先级配置失败", e);
		}
	} 
	/**
	 * 修改权限优先级配置
	 * @param vo
	 * @throws ServiceException
	 */
	public void update(PriorityConf vo) throws ServiceException {
		try {
			 confDao.update(vo);
		} catch (DaoException e) {
			throw new ServiceException("修改优先级配置失败", e);
		}
	}
	/**
	 * 新增优先级配置
	 * @param vo
	 * @throws ServiceException
	 */
	public void add(PriorityConf vo) throws ServiceException {
		try {
			 confDao.insert(vo);
		} catch (DaoException e) {
			throw new ServiceException("新增优先级配置失败", e);
		}
	} 
}
